package com.hawk.eye.server.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hawk.eye.framework.exception.HawkEyeServerException;
import com.hawk.eye.framework.utils.ExceptionAssert;
import com.hawk.eye.framework.utils.ExceptionUtils;
import com.hawk.eye.common.page.PageQuery;
import com.hawk.eye.common.page.PageResult;
import com.hawk.eye.server.convert.OuterWebhookConvert;
import com.hawk.eye.server.domain.dto.req.OuterWebhookReqDTO;
import com.hawk.eye.server.domain.dto.resp.OuterWebhookRespDTO;
import com.hawk.eye.server.domain.po.OuterWebhook;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.hawk.eye.server.mapper.OuterWebhookMapper;
import com.hawk.eye.server.service.IOuterWebhookService;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author salad
 * @date 2023-10-04
 */
@Service
@RequiredArgsConstructor
public class OuterWebhookServiceImpl extends ServiceImpl<OuterWebhookMapper, OuterWebhook> implements IOuterWebhookService {

    private final OuterWebhookMapper outerWebhookMapper;


    @Override
    public List<OuterWebhookRespDTO> getWebhooks(Long serviceId) {
        return OuterWebhookConvert.INSTANCE.toList(outerWebhookMapper.getWebhooks(serviceId));
    }

    @Override
    public OuterWebhookRespDTO selectWebhook(Long serviceId) {
        List<OuterWebhookRespDTO> webhooks = OuterWebhookConvert.INSTANCE.toList(outerWebhookMapper.getWebhooks(serviceId));
        ExceptionAssert.isEmptyThrow(webhooks, () -> new HawkEyeServerException("服务[" + serviceId + "]未配置任何webhook"));
        if (webhooks.size() == 1) {
            return webhooks.get(0);
        }
        //随机选出一个进行推送
        return webhooks.get(ThreadLocalRandom.current().nextInt(webhooks.size()));
    }

    @Override
    public PageResult<OuterWebhookRespDTO> page(PageQuery pageQuery, OuterWebhookReqDTO reqDTO) {
        IPage<OuterWebhook> pages = outerWebhookMapper.page(pageQuery.toPage(),reqDTO);
        return PageResult.of(pages,OuterWebhookConvert.INSTANCE::to);
    }

    @Override
    public OuterWebhookRespDTO detail(Long id) {
        ExceptionUtils.isEmptyThrow(id, "参数[id]不能为空");
        OuterWebhook outerWebhook = getById(id);
        return OuterWebhookConvert.INSTANCE.to(outerWebhook);
    }

    @Override
    public boolean save(OuterWebhookReqDTO reqDTO) {
        OuterWebhook outerWebhook = OuterWebhookConvert.INSTANCE.to(reqDTO);
        return saveOrUpdate(outerWebhook);
    }

    @Override
    public boolean update(OuterWebhookReqDTO reqDTO) {
        ExceptionUtils.isEmptyThrow(reqDTO.getId(), "参数[id]不能为空");
        OuterWebhook outerWebhook = OuterWebhookConvert.INSTANCE.to(reqDTO);
        return saveOrUpdate(outerWebhook);
    }

    @Override
    public List<OuterWebhookRespDTO> webhookList() {
        return OuterWebhookConvert.INSTANCE.toList(list(Wrappers.<OuterWebhook>lambdaQuery()
                .orderByAsc(OuterWebhook::getName)));
    }
}
